<template>
  <div class="app-container">
    <el-tabs v-model="activeName" @tab-click="handleChangeTab">
      <el-tab-pane label="已申请" name="0" />
      <el-tab-pane label="已开票" name="1" />
    </el-tabs>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="申请人角色">
        <!-- <el-input v-model="queryParams.personalName" placeholder="请输入名称" clearable @keyup.enter.native="handleQuery" /> -->
        <el-select v-model="queryParams.applyType" placeholder="请选择申请人角色">
          <el-option label="农场" value="2"></el-option>
          <el-option label="外部合作商" value="0"></el-option>
          <el-option label="外部散户" value="1"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="申请时间" prop="invoiceTime" :label-position="right" label-width="100px">
        <!-- <el-date-picker v-model="queryParams.invoiceTime" type="datetimerange" range-separator="-"
          start-placeholder="开始日期" end-placeholder="结束日期">
        </el-date-picker> -->

        <el-date-picker v-model="applyTime" type="daterange" value-format="yyyy-MM-dd" start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="确认时间" prop="updateTime" v-if="activeName == 1">
        <!-- <el-date-picker v-model="queryParams.invoiceTime" type="datetimerange" range-separator="-"
          start-placeholder="开始日期" end-placeholder="结束日期">
        </el-date-picker> -->

        <el-date-picker v-model="confirmTime" value-format="yyyy-MM-dd" type="daterange" start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary"  size="small " @click="handleQuery">查询</el-button>
        <el-button  size="small " @click="resetQuery">重置</el-button>
        <el-button  disabled size="small "style="color: #fff; background-color: #4cb5ab;" v-if="activeName == 0">已开票</el-button>
      </el-form-item>
    </el-form>


    <el-table border v-loading="loading" :data="invoiceList" @selection-change="handleSelectionChange">

      <el-table-column type="selection"></el-table-column>
      <el-table-column label="申请时间" align="center" prop="invoiceTime" width="180">
        <!-- <template slot-scope="scope">
          <span>{{ parseTime(scope.row.invoiceTime, '{y}-{m}-{d}') }}</span>
        </template> -->
      </el-table-column>
      <el-table-column label="申请人角色" align="center" prop="roleName" />
      <el-table-column label="确认开票时间" align="center" prop="updateTime" v-if="activeName == 1" />
      <el-table-column label="操作人" align="center" prop="updateBy" v-if="activeName == 1" />

      <el-table-column label="总计作业面积" align="center" prop="totalWorkingArea" />
      <el-table-column label="总价" align="center" prop="totalPrice" />
      <el-table-column label="关联订单数" align="center" prop="orderNum" />
      <el-table-column label="开票状态" align="center" prop="invoiceStatus">
        <template #default="scope">
          <span>{{ scope.row.prob == 0 ? '已申请' : '已开票' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleConfireInvoice(scope.row)"
            v-hasPermi="['order:invoice:edit']" v-if="activeName == 0">已开票</el-button>
          <el-button size="mini" type="text" @click="handleCheckDetail(scope.row)"
            v-hasPermi="['order:invoice:remove']">查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 确认开票申请对话框 -->
    <el-dialog title="已确认开票" :visible.sync="dialogVisible" width="30%">
      <span>是否确认开票</span>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmInvoice()">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>

      </span>
    </el-dialog>

    <!-- 查看详情 -->
    <el-dialog title="查看开票信息" :visible.sync="dialogInvoiceVisible" width="50%">
      <div class="invoiceDetail"><span>发票类型</span><span>{{ this.form.invoicingType == 0 ? '电子普通发票' : '电子专用发票' }}</span>
      </div>
      <div class="invoiceDetail"><span>发票抬头</span><span>{{ this.form.invoiceHeader == 0 ? '个人' : '' }}</span></div>
      <div class="invoiceDetail"><span>个人名称</span><span>{{ this.form.personalName }}</span></div>
      <div class="invoiceDetail"><span>收票人手机</span><span>{{ this.form.checkTakerPhone }}</span></div>
      <div class="invoiceDetail"><span>收票人邮箱</span><span>{{ this.form.checkTakerEmail }}</span></div>
      <div class="invoiceClose"><el-button @click="dialogInvoiceVisible = false">关闭</el-button></div>

    </el-dialog>

  </div>
</template>

<script>
import { listInvoice, getInvoice, delInvoice, addInvoice, updateInvoice } from "@/api/order/invoice";

export default {
  name: "Invoice",
  data() {
    return {
      // 选中tab
      activeName: 0,
      // 弹出层
      dialogVisible: false,
      dialogInvoiceVisible: false,
      //选中行中的id号
      selectedId: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单申请开票表格数据
      invoiceAllList: [],
      // 订单申请开票tab列表数据
      invoiceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //申请时间
      applyTime: null,
      //确认时间
      confirmTime: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: null,
        applyUser: null,
        applyType: null,
        totalWorkingArea: null,
        totalPrice: null,
        invoicingType: null,
        invoiceHeader: null,
        personalName: null,
        checkTakerPhone: null,
        checkTakerEmail: null,
        unitName: null,
        dutyNumber: null,
        invoiceTime: null,
        invoiceStatus: null,
        userId: null,
        deptId: null,
        beginTime: '',
        endTime: '',
        params: {
          beginTime: '',
          endTime: ''
        }
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单申请开票列表 */
    getList() {
      this.loading = true;
      this.queryParams.invoiceStatus = this.activeName;
      listInvoice(this.queryParams).then(response => {
        this.invoiceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderId: null,
        applyUser: null,
        applyType: null,
        totalWorkingArea: null,
        totalPrice: null,
        invoicingType: null,
        invoiceHeader: null,
        personalName: null,
        checkTakerPhone: null,
        checkTakerEmail: null,
        unitName: null,
        dutyNumber: null,
        invoiceTime: null,
        invoiceStatus: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null,
        userId: null,
        deptId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if (this.applyTime != null) {
        this.queryParams.params.beginTime = this.applyTime[0];
        this.queryParams.params.endTime = this.applyTime[1];
      } else {
        this.queryParams.params.beginTime = '';
        this.queryParams.params.endTime = '';
      }
      if (this.confirmTime != null) {
        this.queryParams.beginTime = this.confirmTime[0];
        this.queryParams.endTime = this.confirmTime[1];
      } else {
        this.queryParams.beginTime = '';
        this.queryParams.endTime = '';
      }
      this.getList();
    },
    /** 重置按钮操作 */
   resetQuery() {
      this.queryParams.applyType = null;
      this.applyTime = null;
      this.confirmTime = null;
      this.queryParams.params.beginTime = "";
      this.queryParams.params.endTime = "";
      this.queryParams.beginTime = "";
      this.queryParams.endTime = "";
      this.getList();
      
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    handleConfireInvoice(row) {
      this.dialogVisible = true;
      this.selectedId = row.id;
    },
    /** 已开票确认 */
    confirmInvoice(row) {
      let data = {
        id: this.selectedId.toString(),
        invoiceStatus: "1"
      }
      updateInvoice(data).then(response => {
        this.dialogVisible = false;
        this.getList();
      });
    },


    /** 查看详情 */
    handleCheckDetail(row) {
      this.dialogInvoiceVisible = true;
      this.form = row;
    },
    // el-tabs查询按钮
    handleChangeTab(tab) {
      this.activeName = tab.name;
      // this.handleQuery();
      this.queryParams.pageNum = 1;
      this.queryParams.invoiceStatus = this.activeName;
      this.getList();
    },
  }
};
</script>

<style scoped>
.invoiceDetail {
  display: flex;
  align-items: center;
  height: 35px;
}

.invoiceDetail>span:first-child {
  display: inline-block;
  width: 120px;
  text-align: right;
  margin-right: 10px;
}

.invoiceClose {
  height: 35px;
  display: flex;
  justify-content: flex-end;
}
</style>
<style>
.el-dialog__title {
  font-size: 15px;
}
</style>